Public Class FormRoleAED
    Dim departId As Int16
    Public Sub LoadPermission()
        Dim node = New TreeNode()
        node.Text = "System"
        node.Tag = 0
        node.ToolTipText = "System"
        node.Checked = False
        Me.TreeView1.Nodes.Add(node)
        LoadPermission(node)

        checking = True
        For Each childNode As TreeNode In node.Nodes
            If childNode.Checked Then
                node.Checked = True
                Exit For
            End If
        Next
        checking = False
    End Sub

    Public Sub LoadPermission(ByVal parentNode As TreeNode)
        CmdPro.Parameters("@ParentId") = parentNode.Tag
        CmdPro.Parameters("@RoleId") = Me.txtRoleId.Text
        Dim dt = CmdPro.ExecuteDataTable("QueryPermission")
        For Each row In dt.Rows
            Dim node = New TreeNode()
            node.Text = row("PermissionName")
            node.Tag = row("PermissionId")
            node.ToolTipText = row("Note")
            node.Checked = row("Active")
            parentNode.Nodes.Add(node)
            LoadPermission(node)
        Next
    End Sub


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        CmdPro.Begin()
        Try


            If Me.txtRoleName.Text.Trim() = "" Then
                MsgBox("Please input role name!")
                Me.txtRoleName.Focus()
                Exit Sub
            End If

            CmdPro.Parameters("@RoleName") = Me.txtRoleName.Text
            CmdPro.Parameters("@Salary") = Me.txtSalary.Text
            CmdPro.Parameters("@GroupId") = Me.departId

            If txtRoleId.Text = "" Then

                Me.txtRoleId.Text = CmdPro.ExecuteNonQuery("InsertRole", "@id")
                Me.txtRoleId.Text = CmdPro.ExecuteScalar("QueryMaxRoleId")
            Else
                CmdPro.Parameters("@RoleId") = Me.txtRoleId.Text
                CmdPro.ExecuteNonQueryNoParameter("UpdateRoleName")
            End If

            For Each node As TreeNode In Me.TreeView1.Nodes
                SaveNode(node)
            Next
            CmdPro.Commit()
        Catch ex As Exception
            CmdPro.Rollback()
            MsgBox(ex.Message)
        End Try
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub SaveNode(ByRef parentNode As TreeNode)
        CmdPro.Begin()
        Try

            For Each node As TreeNode In parentNode.Nodes
                CmdPro.Parameters("@RoleId") = Me.txtRoleId.Text
                CmdPro.Parameters("@PermissionId") = node.Tag
                CmdPro.Parameters("@Active") = node.Checked
                CmdPro.ExecuteNonQueryNoParameter("SaveNode")
                SaveNode(node)
            Next
            CmdPro.Commit()
        Catch ex As Exception
            CmdPro.Rollback()
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub FormRoleEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadPermission()
    End Sub



    Private checking As Boolean = False

    Private Sub tree_AfterCheck(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles TreeView1.AfterCheck
        If Not checking Then
            checking = True
            downwardApply(e.Node)
            upwardApply(e.Node)
            checking = False
        End If
    End Sub

    Private Sub downwardApply(ByVal node As TreeNode)
        For Each downNode As TreeNode In node.Nodes
            downNode.Checked = node.Checked
            ' apply it down recursively.
            downwardApply(downNode)
        Next
    End Sub

    Private Sub upwardApply(ByVal node As TreeNode)
        If node.Parent IsNot Nothing Then
            If node.Checked Then
                node.Parent.Checked = True
            Else
                Dim allNodesIsUnChecked As Boolean = True
                For Each relativeNode As TreeNode In node.Parent.Nodes
                    If relativeNode.Checked Then
                        allNodesIsUnChecked = False
                    End If
                Next
                If allNodesIsUnChecked Then
                    node.Parent.Checked = False
                End If
            End If

            ' apply it up recursively.
            upwardApply(node.Parent)
        End If
    End Sub

    Private Sub cboDepart_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboDepart.SelectedIndexChanged
        Dim group As GroupInfo = CType(cboDepart.SelectedItem, GroupInfo)
        departId = group.GroupId

    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        For Each obj As Object In Groups.getGroup.Values
            cboDepart.Items.Add(obj)
        Next
    End Sub
End Class